<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="sun.applet.resources.MsgAppletViewer_it"%>
<%@page import="utility.connections"%>
<%@page import="java.sql.*" %>
<%@include file="../../hom/chkUser.jsp"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>SELEZIONA BENE</title>
		<script type="text/javascript">
			function setCategoria()
			{
				dipendente=document.frmFind.dipendente.value; 
				cat = document.frmFind.cmbCat.value;
				location.href = "beni_in_dotazione_agruppo.jsp?cerca=0&order=1&cat="+cat+"&dipendente="+dipendente;
			}
			
			function setSubCategoria()
			{
				dipendente=document.frmFind.dipendente.value;
				cat = document.frmFind.cmbCat.value;
				subcat = document.frmFind.cmbSCat.value;
				location.href = "beni_in_dotazione_agruppo.jsp?cerca=0&order=1&cat="+cat+"&subcat="+subcat+"&dipendente="+dipendente;
			}
			
			function setFornitore()
			{
				dipendente=document.frmFind.dipendente.value;
				cat = document.frmFind.cmbCat.value;
				subcat = document.frmFind.cmbSCat.value;
				fornitore = document.frmFind.cmbFor.value;
				qry="beni_in_dotazione_agruppo.jsp?cerca=0&order=1&cat="+cat+"&subcat="+subcat+"&forn="+fornitore+"&dipendente="+dipendente;
				location.href = qry;
			}
			
			function pickBene(codice)
			{
				dipendente=document.frmFind.dipendente.value;
				qry = "ins_bene.jsp?pick="+codice+"&dipendente="+dipendente+"&dest=3";
				location.href = qry;
			}
			
			function getBene()
			{
				dipendente=document.frmFind.dipendente.value;
				codice = 			document.frmFind.txtCodice.value;
				categoria = 		document.frmFind.cmbCat.value;
				sottocategoria = 	document.frmFind.cmbSCat.value;
				fornitore = 		document.frmFind.cmbFor.value;
				qry = "beni_in_dotazione_agruppo.jsp?cerca=1&order=1&cat="+categoria+"&cod="+codice+"&subcat="+sottocategoria+"&forn="+fornitore+"&dipendente="+dipendente;
				location.href = qry;
			}
			
			function back()
			{
				location.href="../hdip.jsp";
			}
			
			function setOrder(index)
			{
				dipendente=document.frmFind.dipendente.value;
				codice = 			document.frmFind.txtCodice.value;
				categoria = 		document.frmFind.cmbCat.value;
				sottocategoria = 	document.frmFind.cmbSCat.value;
				fornitore = 		document.frmFind.cmbFor.value;
				qry = "beni_in_dotazione_agruppo.jsp?cerca=1&order="+index+"&cat="+categoria+"&cod="+codice+"&subcat="+sottocategoria+"&forn="+fornitore+"&dipendente="+dipendente;
				location.href = qry;
			}
		</script>
		<script type="text/javascript" src="../js/immagini.js" ></script>
	    <style type="text/css">
<!--
.style1 {color: #000000}
-->
        </style>
</head>
	<link href="../../layout.css" rel="stylesheet" type="text/css" media="screen" />
	<body>
	<%!
		String cod;
		String cat;
		String subcat;
		String forn;
		String user;
		String cerca;
		String dipendente;
	 %>
	 <%	dipendente="" + request.getParameter("dipendente"); %>
		<form name="frmFind" method="post" action="javascript:getBene();">
		  <table width="100%" border="0" cellspacing="1" cellpadding="0">
	        <tr>
	          <td width="16%"><div align="right">CODICE:</div></td>
	          <td width="84%"><input type="text" name="txtCodice"></td>
	        </tr>
	        <tr>
	        <input type="text" name="dipendente" value=<%=dipendente %> width="300" disabled="disabled" style="visibility: hidden;" />
	        </tr>
	        <tr>
	          <td><div align="right">CATEGORIA:</div></td>
	          <td>
			  	<select name="cmbCat" onChange="setCategoria();">
					<option value="">SELEZIONA UNA CATEGORIA</option>
					<%
						cerca  = "" + request.getParameter("cerca");
						cod    = "" + request.getParameter("cod");
						cat    = "" + request.getParameter("cat");
						subcat = "" + request.getParameter("subcat");
						forn   = "" + request.getParameter("forn"); 
						user = "" + session.getAttribute("user").toString().toLowerCase();
						dipendente="" + request.getParameter("dipendente");
						
						Connection  conn;
						Statement   st;
						ResultSet   rs;
						String 		qry;
						conn=connections.getConnection();
						st=connections.getStatement();
						qry = "SELECT * FROM CATEGORIABENE ORDER BY SIGLA";
						out.println(qry);
						rs = st.executeQuery(qry);
						while (rs.next())
						{
							String strCat = rs.getString("SIGLA");
							if(cat.equalsIgnoreCase(strCat))
								out.println("<option selected =\"selected\" value=\"" + strCat + "\">" + strCat + " </option>");
							else
								out.println("<option value=\"" + strCat + "\">" + strCat + "</option>");
						}		
						rs.close();
						st.close();
					%>
	          	</select>          
			  </td>
	        </tr>
	        <tr>
	          <td><div align="right">SOTTOCATEGORIA:</div></td>
	          <td>
			  	<select name="cmbSCat" onChange="setSubCategoria();">
					<option value="">SELEZIONA UNA SOTTOCATEGORIA</option>
					<%
						if(!cat.equals(""))
						{
							conn=connections.getConnection();
							st=connections.getStatement();
							qry = "SELECT * FROM SOTTOCATEGORIABENE WHERE CATEGORIABENE='"+cat+"' ORDER BY NOME";
							rs = st.executeQuery(qry);
							while (rs.next())
							{
								String strSubCat = rs.getString("codice");
								if(strSubCat.equalsIgnoreCase(subcat))
									out.println("<option selected=\"selected\" value=\"" + rs.getString("CODICE") + "\">" + rs.getString("NOME") + "</option>");
								else
									out.println("<option value=\"" + rs.getString("CODICE") + "\">" + rs.getString("NOME") + "</option>");
							}
							rs.close();
							st.close();
						}
					%>
				</select>
			  </td>
	        </tr>
	        <tr>
	          <td><div align="right">FORNITORE:</div></td>
	          <td>
			  	<select name="cmbFor" onChange="setFornitore();">
					<option value="">SELEZIONA UN FORNITORE</option>
					<%
						conn=connections.getConnection();
						st=connections.getStatement();
						qry = "SELECT PARTITAIVA,NOMEORGANIZZAZIONE FROM FORNITORE ORDER BY NOMEORGANIZZAZIONE";
						rs = st.executeQuery(qry);
						while (rs.next())
						{
							String f = rs.getString(1);
							if(f.equalsIgnoreCase(forn))
								out.println("<option selected=\"selected\" value=\"" + rs.getString(1) + "\">" + rs.getString(2)+ "</option>");
							else
								out.println("<option value=\"" + rs.getString(1) + "\">" + rs.getString(2)+ "</option>");
						}
						rs.close();
						st.close();
					%>
				</select>
			  </td>
	        </tr>
	        <tr>
	          <td><div align="right"></div></td>
	          <td><input type="submit" name="cmdCerca" value="CERCA" > 
	              <input type="submit" name="cmdBack" value="INDIETRO" onClick="this.form.action='../hdip.jsp'"></td>
	        </tr>
	      </table>
		</form>
		<p align="center">ELENCO BENI DISPONIBILI </p>
		<table width="100%" border="1" cellspacing="1" cellpadding="0" style="border:1px solid;">
          <tr>
            <td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1">PROG.</span></td>
            <td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1"><a href="javascript:setOrder(1)">INV.</a></span></td>
			<td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1"><a href="javascript:setOrder(2)">SER.</a></span></td>
			<td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1"><a href="javascript:setOrder(3)">IMPORTO</a></span></td>
			<td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1"><a href="javascript:setOrder(4)">ACQ.</a></span></td>
			<td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1"><a href="javascript:setOrder(5)">GAR</a></span></td>
			<td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1"><a href="javascript:setOrder(6)">ATT.</a></span></td>
			<td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1"><a href="javascript:setOrder(7)">SCAD.</a></span></td>
			<td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1"><a href="javascript:setOrder(8)">CONF.</a></span></td>
			<td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1"><a href="javascript:setOrder(9)">OBS.</a></span></td>
			<td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1"><a href="javascript:setOrder(10)">SOTT.</a></span></td>
			<td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1"><a href="javascript:setOrder(11)"/>FORNITORE</a></span></td>
			<td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1"><a href="javascript:setOrder(12)"/>TARGH.</a></span></td>
			<td align="center" bgcolor="#FFFFFF" style="border:1px solid;" ><span class="style1"><a href="javascript:setOrder(13)"/>DESCRIZIONE</a></span></td>
          </tr>
          <%
				//out.println(cerca + ", " + cod + ", " + cat + ", " + subcat + ", " + forn + ", " + user);
          		
          		System.out.println( request.getParameter("dipendente"));
          		System.out.println(dipendente);
				if(cerca.equals("1"))
				{
					conn=connections.getConnection();
					st=connections.getStatement();
					int order = Integer.parseInt(request.getParameter("order"));
					
					String subQry = "";
					if(cod.length()>0)
						subQry = " B.NUMEROINVENTARIOGENERICO = " + cod + " AND ";
					else
					{
						if(cat.length()>0)
							subQry = subQry + " SC.CATEGORIABENE = '" + cat + "' AND ";
						if(subcat.length()>0)
							subQry = subQry + " B.SOTTOCATEGORIABENE = " + subcat + " AND ";
						if(forn.length()>0)
							subQry = subQry + " B.FORNITORE = " + forn + " AND ";
					}
					if (subQry.length()>0)
						subQry = " AND " + subQry.substring(1,subQry.length()-4);
					
					String restrict= "SELECT ASSEGNAZIONE.BENE FROM ASSEGNAZIONE WHERE ASSEGNAZIONE.GRUPPODILAVORO IN (SELECT ALLOCAZIONE.GRUPPODILAVORO FROM ALLOCAZIONE WHERE ALLOCAZIONE.DIPENDENTE='"+dipendente+"' AND ALLOCAZIONE.DATAINIZIO<=SYSDATE AND ALLOCAZIONE.DATAFINE>=SYSDATE)";
					qry = "SELECT B.NUMEROINVENTARIOGENERICO,B.NUMEROINVENTARIOSERIALE,TO_CHAR(B.IMPORTO,'999,999.99'),TO_CHAR(B.DATAACQUISTO,'DD/MM/YYYY')," + 
					             "B.GARANZIA,TO_CHAR(B.DATAATTIVAZIONE,'DD/MM/YYYY'),TO_CHAR(B.DATASCADENZA,'DD/MM/YYYY'),B.CONFORME," + 
					             "B.OBSOLETO,SC.NOME AS SOTTOCATEGORIA,F.NOMEORGANIZZAZIONE AS FORNITORE,B.TARGHETTA,B.DESCRIZIONE " + 
					             "FROM BENE B, SOTTOCATEGORIABENE SC, FORNITORE F " + 
					             "WHERE B.NUMEROINVENTARIOGENERICO IN ("+restrict+") AND SC.CODICE=B.SOTTOCATEGORIABENE AND F.PARTITAIVA=B.FORNITORE " +
					             subQry +
					             "ORDER BY " + order;
					//out.println("<p>" + qry + "</p>");
					//out.println("<p>SUBQUERY LENGHT = " + subQry.length()+ "</p>");
					System.out.println(qry);
					rs = st.executeQuery(qry);
					int i=0;
					
					if(rs.isBeforeFirst()&&rs.isAfterLast())
					{
						//Avvisare che non ci sono record!!!!
					}
					else
					{
						while (rs.next())
						{
							String color;
		              		String bordercell;
		              		
		              		i++;
		              		
		              		if (i%2==0)
		              		{
		              			color ="\"#E0E0E0\"";
		              			bordercell="style=\"border:1px solid;\";";
		              		}
		              		else
		              		{
		              			color = "\"#EFFFEF\"";
		              			bordercell="style=\"border:1px solid;\";";
		              		}
		              		
		              		out.println("<tr>");
		              		out.println("<td align=\"right\""+bordercell+" bordercolor=\"#555555\" bgcolor = "+color+ "><span class=\"style1\">" + i + "</span></td>"); //prog.
		              		for(int col=1;col<14;col++)
		              		{
		              			if (col==1)
		              			{
		              				String id=rs.getString("numeroinventariogenerico");
		              				out.println("<td align=\"right\""+bordercell+" bordercolor=\"#555555\" bgcolor = "+color+ "><span class=\"style1\"><a href=\"javascript:pickBene("+id+")\">"+id+"</span></td>");
		              			}
		              			else if (col==5||col==8||col==9||col==12)
		              			{
		              				String chk= "";
		              				if (rs.getString(col).equals("1")) chk = "checked"; else chk="";
		              				out.println("<td align=\"center\""+bordercell+" bordercolor=\"#555555\" bgcolor = "+color+ "><input type = \"checkbox\" " + chk + " disabled></td>");
		              			}
		              			else
		              			{
		              				String alignment="";
		              				if(col==4||col==6||col==7)
		              					alignment="center";
		              				else
		              					alignment="right";
		              				out.println("<td align=\""+alignment+"\""+bordercell+" bordercolor=\"#555555\" bgcolor = "+color+ "><span class=\"style1\">" + rs.getString(col)+ "</span></td>");
		              			}
		              		}
		              		out.println("</tr>");
						}
					}
				}
				
				rs.close();
				st.close();
             %>
        </table>
		<p>&nbsp;</p>
	</body>
</html>